package cn.fanqinghong.ymcc.web.controller;

import cn.fanqinghong.ymcc.dto.SmsDto;
import cn.fanqinghong.ymcc.exception.GlobalException;
import cn.fanqinghong.ymcc.result.JSONResult;
import cn.fanqinghong.ymcc.service.ICommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * FAN
 * 2022/7/15  19:40
 */
@RestController
@RequestMapping("/verifycode")
public class CommonController {
    @Autowired
    private ICommonService commonService;


    /**
     * 获取图形验证码
     * @param key
     * @return
     */
    @GetMapping("/imageCode/{key}")
    public JSONResult imageCode(@PathVariable("key") String key){
        try {
            return JSONResult.success(commonService.imageCode(key));
        }catch (GlobalException e){
            e.printStackTrace();
            return JSONResult.error(e.getMessage(),e.getCode());
        }
        catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("获取失败");
        }

    }

    /**
     * 发送短信
     * @param dto
     * @return
     */
    @PostMapping("/sendSmsCode")
    public JSONResult smsCode(@RequestBody SmsDto dto){
            commonService.smsCode(dto);
            return JSONResult.success();

    }
}
